$(function(){
	
});

var workerIds = "";


var selectProjectNum='0';
function addProjectSelect(){
	CreateProjectList(selectProjectNum++);
}
function CreateProjectList(num){
    $.ajax({
        url: ctp + "/groupController/getProjectList.shtml", 
        data: {},
        type: "POST", 
        cache : false, 
        dataType: "json",
        success: function(data) {
            var list = data.list; 
			var $str='<div class="layui-input-inline" id="div_project'+num+'">';
			$str +='<select class="smart-body_select isgroup-add" name="project'+num+'" id="project'+num+'">';
			$str +='<option value="">--请选择--</option>';
        	for (var i = 0; i < list.length; i++) {
        		var l = list[i];
        		$str+='<option value="'+l.REC_ID+'">'+l.NAME+'</option>';
        	}
        	$str+='</select><button id="btn'+num+'" class="layui-btn" onclick="delProject(\'div_project'+num+'\');" value="删除"><i class="layui-icon">&#xe640;</i></button>';
        	$str+='</div>';
        	$('#projects').append($str);
        }
    });
}


function delProject(project){
	$('#'+project).remove();
}



function groupAdd(){
	$("#workerIds").val(workerIds);
	setTagAndValue('form1','projectIds',getProjectStr());
	if (!Validator.Validate(document.getElementById("form1"), 1)) {
		return;
	}
	$.ajax({ 
	 	url: ctp + "/groupController/groupSave.shtml",
	    data: $('#form1').serialize(),
		type : 'POST',
		async:false, 
		success : function(result) {  
			if(result=='1') {  
				jalert9("保存成功！");
				setTimeout(function(){
					location.href=ctp + "/groupController/group.shtml";
				},WARN_TIME_OUT);
			}else if(result=='0') {     
				jalert("保存失败，请联系管理员！");   
			} else if(result=='2'){
				jalert("班组名称已存在，请更换！");
			} else if(result=='3'){
				jalert("所选班长已经是其他班组班长，请更换！");
			}
  		}
	});
	
}

//获取所选项目id组合的字符串，以逗号分隔
function getProjectStr(){
	var str='';
	$('select[id^="project"]').each(function(){
		str+=$(this).val()+',';
	});
	return str;
}


function selectWorker(){
	layui.layer.open({
        type: 2,
        area: ['1000px', '500px'],
        title: "选择工人",
        fixed: false,
        maxmin: false,
        content: ctp + "/groupController/selectWorkerPage.shtml"
    });
}

function selectMonitor(){
	layui.layer.open({
		type: 2,
		area: ['1000px', '500px'],
		title: "选择班长",
		fixed: false,
		maxmin: false,
		content: ctp + "/groupController/selectMonitorPage.shtml"
	});
}

function selectProjects(){
	layui.layer.open({
		type: 2,
		area: ['1000px', '450px'],
		title: "选择项目",
		fixed: false,
		maxmin: false,
		content: ctp + "/groupController/selectProjectPage.shtml"
	});
}

function addMonitor(monitorId,monitorName,tel){
	$('#monitor').val(monitorName);
	$('#monitorId').val(monitorId);
	$('#tel').val(tel);
}

function addProjects(projectIds,projectNames){
	$('#projectIds').val(projectIds);
	$('#projectNames').val(projectNames);
}



/******************************************************************/
var workIdsArray = new Array();//选中的工人
function Array2Str(){
	var str='';
	for(var i=0;i<workIdsArray.length;i++){
		str+=workIdsArray[i]+',';
	}
	return str;
}

function removeFromWorkIdsArray(id){
	for(var i=0;i<workIdsArray.length;i++){
		if(workIdsArray[i]==id){
			workIdsArray.splice(i,1);
		}			
	}
}

function searchData1(curr) {
	addWorkers(workerIds,curr)
}

function getworkerIds(recId,curr){
	if(workerIds == "nono"){
		workerIds = "";
	}
	if(workerIds == ""){
		workerIds+=recId;	
	}else{
		workerIds+=","+recId;
	}
	addWorkers(workerIds,curr);
}

function addWorkers(workerIds,curr){
	if(workerIds == ""){
		workerIds = "nono";
	}
	setTagAndValue("form2","workerIds",workerIds);
	//---开始的页数
	setTagAndValue("form2","curr",curr); 
	//---每页几条数据
	setTagAndValue("form2","SUNNADA_PAGE_SIZE",5); 
	$.ajax({
		url: ctp + "/groupController/getGroupWorkerList.shtml", 
		data: $('#form2').serialize(),
		type: "POST", 
		cache : false, 
		dataType: "json", 
		success: function(data) {
		    var list = data.list; 
		    var pageJson = data.params;
		    $("#tbodyData").html("");
			for (var i = 0; i < list.length; i++) {
				var l = list[i];
				$str="";
				$str+="<tr id='"+l.REC_ID+"'><td>"+l.NAME+"</td>";
				$str+="<td>"+l.JOBNO+"</td>";
				$str+="<td>"+l.WORKTYPE+"</td>";
				if(l.GROUPNAME==undefined){
					$str+= "<td>暂无班组</td>";
				}else{
					$str+= "<td>"+l.GROUPNAME+"</td>";
				}
				
				$str+= '<td><a onClick="removeWorker(\''+l.REC_ID+'\')" class="smart-table_operation">移除</a></td></tr>';
				$("#tbodyData").append($str);
			}
			$('#appNum').html(pageJson.rc);
			 //------------   分页开始  -------------
			 getPaging2(pageJson,list,1,1);
		    //------------  分页结束      -------------
	    }

	});
}


function removeWorker(recId){
	jconfirm("del('" + recId + "')", "确定要移除所选工人吗？");	
}


function del(id){
	var items =workerIds.split(",");
	var recIds = "";
	for (var i = 0; i < items.length; i++) {
		if ( id !=items[i]) {
			recIds += items[i]+",";
		}
	}
	 var reg = /,$/gi;
	 workerIds = recIds.replace(reg, "");
	 searchData1(0);
}

/*****************************************************************************************************************/

function back(){
	//location.href=ctp + "/groupController/group.shtml";
	history.back();
}



